package com.sgxy.wmservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sgxy.commonutils.R;
import com.sgxy.wmservice.entity.WmGoods;
import com.sgxy.wmservice.entity.WmOrders;
import com.sgxy.wmservice.entity.vo.OrdersVo;
import com.sgxy.wmservice.service.WmOrdersService;
import com.sgxy.wmservice.service.WmSupplierService;
import com.sgxy.wmservice.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-29
 */
@RestController
@RequestMapping("/wmservice/wmorders")
public class WmOrdersController {
    @Autowired
    private WmOrdersService wmOrdersService;


    //获取分页订单列表
    @PostMapping("{page}/{limit}")
    public R index(@PathVariable Long page,
                   @PathVariable Long limit,
                   @RequestBody(required = false) WmOrders wmOrders){
        Page<OrdersVo> ordersPage = new Page<>(page,limit);
        //QueryWrapper<WmOrders> ordersQueryWrapper = new QueryWrapper<>();
        ////TODO 条件查询
        //if (!StringUtils.isEmpty(wmOrders.getOrderNo())) {
        //    ordersQueryWrapper.like("order_no", wmOrders.getOrderNo());
        //}
        //wmOrdersService.page(ordersPage, ordersQueryWrapper);
        wmOrdersService.selectPageVoList(ordersPage,wmOrders);
        return R.ok().data("items", ordersPage.getRecords()).data("total", ordersPage.getTotal());
    }
    @GetMapping("getOrderById/{id}")
    public R getOrderById(@PathVariable String id){
        OrdersVo ordersVo = wmOrdersService.selectOneById(id);
        return R.ok().data("ordersVo",ordersVo);
    }
    @GetMapping("getOrdersAll")
    public R getRoleAll(){
        List<WmOrders> list = wmOrdersService.list(null);
        return R.ok().data("list",list);
    }
    ////获取根据id获取订单
    //@GetMapping("getorder/{id}")
    //public R getOrderById(@PathVariable String id){
    //    WmOrders order = wmOrdersService.getById(id);
    //    return R.ok().data("order",order);
    //}

    //更新订单
    @PostMapping("updateOrder")
    public R updateOrder(@RequestBody WmOrders wmOrders){
        wmOrdersService.updateById(wmOrders);
        return R.ok();
    }

    //添加订单
    @PostMapping("addOrders")
    public R addOrders(@RequestBody WmOrders wmOrders){
        wmOrders.setOrderNo(OrderNoUtil.getOrderNo());
        wmOrdersService.save(wmOrders);
        return R.ok();
    }
    //删除
    @DeleteMapping("{id}")
    public R deleteOrders(@PathVariable String id){
        wmOrdersService.deleteById(id);
        return R.ok();
    }


}

